import smtplib
from email.header import Header
from email.mime.text import MIMEText

def send(to_email,  email_title, email_content):
    """
    :param to_email: 发送邮箱的对象
    :param email_title: 邮件标题
    :param email_content: 邮件内容
    :return "邮件发送失败" or "邮件发送失败"
    """

    mail_user = "2284182343@qq.com"
    mail_pwd = "ezpqkyhvmgtadjjh"
    mail_sender = "2284182343@qq.com"
    mail_receiver =  [to_email]
    receivers = []
    mail_receiver = receivers + [to_email]

    # 发送邮件
    return sendMail(mail_user, mail_pwd, mail_sender,
             mail_receiver, email_content, email_title)


# print(sim)
def sendMail(mail_user, mail_pwd, sender, receiver, content, title):
    """
    说明：此函数实现发送邮件功能。
    :param user: 用户名
    :param pwd: 授权码
    :param sender: 发送方
    :param receiver: 接收方
    :param content: 邮件的正文
    :param title: 邮件的标题
    :return:
    """
    mail_host = "smtp.qq.com"  # qq的SMTP服务器
    # 第一部分：准备工作
    # 1.将邮件的信息打包成一个对象
    message = MIMEText(content, "plain", "utf-8")  # 内容，格式，编码
    # 2.设置邮件的发送者
    message["From"] = sender
    # 3.设置邮件的接收方
    # message["To"] = receiver
    # join():通过字符串调用，参数为一个列表
    message["To"] = ",".join(receiver)
    # 4.设置邮件的标题
    message["Subject"] = title

    # 第二部分：发送邮件
    try:
        # 1.启用服务器发送邮件
        # 参数：服务器，端口号
        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
        # 2.登录邮箱进行验证
        # 参数：用户名，授权码
        smtpObj.login(mail_user, mail_pwd)
        # 3.发送邮件
        # 参数：发送方，接收方，邮件信息
        smtpObj.sendmail(mail_user, receiver, message.as_string())
        return "邮件发送成功"
    except:
        return '邮件发送失败'

